/*
 * 类名称:TranferServiceImpl.java
 * 包名称:com.platform.service.impl
 *
 * 修改履历:
 *     日期                       修正者        主要内容
 *     2019-10-13 23:57:17        lipengjun     初版做成
 *
 * Copyright (c) 2019-2019 微同科技
 */
package com.platform.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.entity.WxuserOrderEntity;
import com.platform.service.WxuserOrderService;
import com.platform.utils.Query;
import com.platform.utils.QueryPlus;
import com.platform.dao.TranferDao;
import com.platform.entity.TranferEntity;
import com.platform.service.TranferService;
import com.platform.utils.RRException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 申请提现记录表Service实现类
 *
 * @author lipengjun
 * @date 2019-10-13 23:57:17
 */
@Service("tranferService")
public class TranferServiceImpl extends ServiceImpl<TranferDao, TranferEntity> implements TranferService {
    @Autowired
    private TranferDao tranferDao;
    @Autowired
    private WxuserOrderService wxuserOrderService;

    @Override
    public List<TranferEntity> queryAll(Map<String, Object> params) {
        return baseMapper.queryAll(params);
    }

    @Override
    public Page queryPage(Map<String, Object> params) {
        //排序
        params.put("sidx", "T.id");
        params.put("asc", false);
        Page<TranferEntity> page = new QueryPlus<TranferEntity>(params).getPage();
        return page.setRecords(baseMapper.selectTranferPage(page, params));
    }

    @Override
    public boolean add(TranferEntity tranfer) {
        return this.save(tranfer);
    }

    @Override
    @Transactional
    public boolean update(TranferEntity tranfer) {
        if(tranfer.getStatus() == 1){
            //关联修改订单状态
            WxuserOrderEntity wxuserOrderEntity = wxuserOrderService.queryObjectByOrderSn(tranfer.getOrderSn());
            if(wxuserOrderEntity!=null){
                wxuserOrderEntity.setTranferStatus(1);
                int update = wxuserOrderService.update(wxuserOrderEntity);
                if(update<0){
                    throw new RRException("订单修改失败！");
                }
            }else {
                throw new RRException("订单不存在！");
            }
        }
        return this.updateById(tranfer);
    }

    @Override
    public boolean delete(Integer id) {
        return this.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(Integer[] ids) {
        return this.removeByIds(Arrays.asList(ids));
    }

    @Override
    public List<TranferEntity> queryList(Map<String, Object> map) {
        return tranferDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return tranferDao.queryTotal(map);
    }
}
